package com.a.module_storage;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.myframe.DistDeptApp;
import com.a.lib_base.base.ui.BaseMVIActivity;
import com.a.lib_bean.api.ParamsApi;
import com.a.lib_network.bean.HttpData;
import com.a.lib_route.storage.StorageRouter;
import com.a.lib_utils.TipUtil;
import com.a.module_storage.databinding.ActivityStorageBinding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 库存
 */
@Route(path = StorageRouter.PATH_MAIN, group = StorageRouter.GROUP)
public class StorageActivity extends BaseMVIActivity<ActivityStorageBinding> implements View.OnClickListener {
    private static final String TAG = StorageActivity.class.getSimpleName();

    private int index = 1;
    private String classTypeNo = "";
    private ProductItemAdapterKt adaptbroduct;
    private ProductTypeAdapter adaptbroductType;
    private final List<ProductItem> productItemList = new ArrayList<>();
    private final List<ProductType> productTypeList = new ArrayList<>();
//    private final List<Integer> wHID = new ArrayList<>();
    private Integer sourceType;//仓库
    //搜索
    private boolean isSearch = false;
    private String searchStr = "";
    private StorageRequester storageRequester;

    @Autowired
    String typeUrl;
    @Autowired
    String itemUrl;

    @Override
    public ActivityStorageBinding viewBinding(ViewGroup view) {
        return ActivityStorageBinding.inflate(getLayoutInflater());
    }

    @Override
    public void initViewModel() {
        storageRequester = getActivityOwnerViewModel(StorageRequester.class);
    }

    @Override
    protected void onInitView() {
        ARouter.getInstance().inject(this);
        b.tvStorage1.setTextColor(this.getResources().getColor(R.color.themeColor));
        b.rvProductType.setLayoutManager(new LinearLayoutManager(this));
        adaptbroductType = new ProductTypeAdapter(productTypeList, this, handler);
        b.rvProductType.setAdapter(adaptbroductType);
        b.rvProduct.setLayoutManager(new LinearLayoutManager(this));
        adaptbroduct = new ProductItemAdapterKt(productItemList);
        b.rvProduct.setAdapter(adaptbroduct);
        b.tvStorage1.setOnClickListener(this);
        b.tvStorage2.setOnClickListener(this);
        b.tvStorage3.setOnClickListener(this);
        b.tvStorage4.setOnClickListener(this);
        b.tvSearch.setOnClickListener(this);
        b.imgBackIv.setOnClickListener(this);
        //设置下滑到底部的监听
//        rv_product.addOnScrollListener(new OnRecyclerViewScrollListener() {
//            @Override
//            public void onBottom() {
//                super.onBottom();
//                if (productItemList.size() != 0 && productItemList.size() % 15 == 0) {//判断目前商品列表是否能够取余15取整
//                    doGetProductItem(classTypeNo);
//                    Log.d(TAG, "滑到了末端");
//                }
//
//            }
//        });
    }

    @Override
    protected void onInitData() {
        changeWH(4);
        classTypeNo = "";
        index = 1;
        doGetProductType();
        doGetProductItem(classTypeNo);
    }

    @Override
    protected void onOutput() {
        storageRequester.output(this, event -> {
            final HttpData<StorageEvent.Result> result = event.result;
            if (!result.isSuccess()) {
                TipUtil.failTip(StorageActivity.this, result.getMsg());
                return null;
            }
            final int eventId = event.eventId;
            if (eventId == StorageEvent.EVENT_GET_LIST) {
                int preNotifyCount = productItemList.size();
                if (index == 1) {  //如果index为1 ，说明第一次请求。
                    b.rvProduct.scrollToPosition(0);
                    productItemList.clear();
                    adaptbroduct.notifyItemRangeRemoved(0, preNotifyCount);
                }
                index++;
                productItemList.addAll(result.getData().productItemList);
                adaptbroduct.notifyDataSetChanged();
            } else if (eventId == StorageEvent.GET_TYPE_LIST) {
                b.rvProductType.scrollToPosition(0);
                productTypeList.clear();
                productTypeList.addAll(result.getData().productTypeList);
                adaptbroductType.notifyDataSetChanged();
            }
            return null;
        });
    }

    @Override
    protected void onInput() {
    }


    private void changeWH(int sourceType) {
        this.sourceType = sourceType;
//        wHID.clear();
//         TODO: 2022/7/23 删除了不明所以的东西
//        switch (sourceType) {
//            case 4:
//                wHID.add(76);
//                wHID.add(99);
//                break;
//            case 3:
//                wHID.add(75);
//                wHID.add(347);
//                wHID.add(353);
//                wHID.add(101);
//                wHID.add(357);
//                wHID.add(348);
//                wHID.add(354);
//                wHID.add(103);
//                break;
//            case 2:
//                wHID.add(74);
//                wHID.add(101);
//                wHID.add(350);
//                wHID.add(353);
//                wHID.add(355);
//                wHID.add(357);
//                break;
//            case 1:
//                wHID.add(73);
//                wHID.add(103);
//                wHID.add(203);
//                wHID.add(346);
//                wHID.add(350);
//                wHID.add(357);
//                break;
//        }

    }

    private void doGetProductType() {
        Map<String, String> params = new HashMap<>();
        params.put("employeeID", DistDeptApp.getEmployee().getEmployeeID() + "");//传入员工ID
        params.put("sourceType", sourceType + "");
        params.put("isStorage", "1");
        storageRequester.input(new StorageEvent(StorageEvent.GET_TYPE_LIST).setApi(new ParamsApi(typeUrl, params)));
    }

    public void doSearch() {
        index = 1;
        if (!b.etSearch.getText().toString().equals("")) {
            isSearch = true;
            searchStr = b.etSearch.getText().toString();
        }
        classTypeNo = "";
        doGetProductItem(classTypeNo);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.img_backIv) {
            finish();
        } else if (id == R.id.tv_storage1) { //成品仓
            b.tvStorage1.setTextColor(getResources().getColor(R.color.themeColor));
            b.tvStorage2.setTextColor(getResources().getColor(R.color.grey));
            b.tvStorage3.setTextColor(getResources().getColor(R.color.grey));
            b.tvStorage4.setTextColor(getResources().getColor(R.color.grey));
            changeWH(4);
            classTypeNo = "";
            index = 1;
            isSearch = false;
            doGetProductType();
            doGetProductItem(classTypeNo); //classTypeNo = "" 表示获取全部的商品
        } else if (id == R.id.tv_storage2) {//半成品仓
            b.tvStorage1.setTextColor(getResources().getColor(R.color.grey));
            b.tvStorage2.setTextColor(getResources().getColor(R.color.themeColor));
            b.tvStorage3.setTextColor(getResources().getColor(R.color.grey));
            b.tvStorage4.setTextColor(getResources().getColor(R.color.grey));
            changeWH(3);
            classTypeNo = "";
            index = 1;
            isSearch = false;
            doGetProductType();
            doGetProductItem(classTypeNo);
        } else if (id == R.id.tv_storage3) { //辅料
            b.tvStorage1.setTextColor(getResources().getColor(R.color.grey));
            b.tvStorage2.setTextColor(getResources().getColor(R.color.grey));
            b.tvStorage3.setTextColor(getResources().getColor(R.color.themeColor));
            b.tvStorage4.setTextColor(getResources().getColor(R.color.grey));
            changeWH(2);
            classTypeNo = "";
            index = 1;
            isSearch = false;
            doGetProductType();
            doGetProductItem(classTypeNo);
        } else if (id == R.id.tv_storage4) {//原料
            b.tvStorage1.setTextColor(getResources().getColor(R.color.grey));
            b.tvStorage2.setTextColor(getResources().getColor(R.color.grey));
            b.tvStorage3.setTextColor(getResources().getColor(R.color.grey));
            b.tvStorage4.setTextColor(getResources().getColor(R.color.themeColor));
            changeWH(1);
            classTypeNo = "";
            index = 1;
            isSearch = false;
            doGetProductType();
            doGetProductItem(classTypeNo);
        } else if (id == R.id.tv_search) {
            doSearch();
        }
    }


    private final Handler handler = new Handler(msg -> {
        if (msg.what == 1) {
            classTypeNo = msg.getData().getString("classTypeNo");
            index = 1;
            isSearch = false;
            doGetProductItem(classTypeNo);
            Log.d(TAG, "doGetProductItem+++++++:" + classTypeNo);
        }
        return false;
    });


    private void doGetProductItem(String classTypeNo) {
        StringBuilder stringBuilder = new StringBuilder("?");
//        for (Integer integer : wHID) {
//            stringBuilder.append("wHID").append("=").append(integer).append("&");
//        }
//        stringBuilder.append("wHID").append("=").append("''").append("&");
        stringBuilder.append("classTypeNo").append("=").append(classTypeNo)
                .append("&").append("sourceType").append("=").append(sourceType)
                .append("&").append("index").append("=").append(index);
        if (isSearch) { //判断是否通过搜索
            stringBuilder.append("&").append("filter").append("=").append(b.etSearch.getText().toString());
        }
        storageRequester.input(new StorageEvent(StorageEvent.EVENT_GET_LIST).setApi(new ParamsApi(itemUrl, stringBuilder.toString())));
    }

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, StorageActivity.class);
        context.startActivity(intent);
    }


}
